﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Minestorm.Retro.Entities
{
    public class Explosion : Entity
    {
        private Vector3[] _explodeVectors, _currentExplosionVectors;
        private int _currentExplodeStep = 0;

        public Explosion(GraphicsDevice graphics, Vector2 position)
            : base(graphics, position)
        {
            
        }

        public override void Initialize()
        {
            int lineCount = RandomHelper.GetExplosionLineCount();
            _explodeVectors = new Vector3[lineCount + 1];
            _currentExplosionVectors = new Vector3[lineCount + 1];
            short[] indicies = new short[lineCount * 2];
            for (short i = 0; i < lineCount; i++)
            {
                _explodeVectors[i] = RandomHelper.GetExplosionLineVector();
                indicies[i * 2] = 0;
                indicies[i * 2 + 1] = (short)(i + 1);
            }
            SetIndicies("Explosion", false, indicies);
        }

        public void SetupVertices()
        {
            var vertices = new VertexPositionColor[_explodeVectors.Length];

            vertices[0] = new VertexPositionColor(Vector3.Zero, Constants.VectorColor);

            for (var i = 1; i < _explodeVectors.Length; i++)
            {
                _currentExplosionVectors[i] += _explodeVectors[i];
                vertices[i] = new VertexPositionColor(_currentExplosionVectors[i], Constants.VectorColor);
            }

            SetVerticies(vertices);

            _currentExplodeStep++;

            if (_currentExplodeStep == 20)
                ShouldDraw = false;
        }

        public override Matrix GetWorld(string indexBufferName)
        {
            return Matrix.Identity * Matrix.CreateTranslation(new Vector3(Position, 0f));
        }

        public void Move()
        {
            var vertices = new VertexPositionColor[_explodeVectors.Length];

            vertices[0] = new VertexPositionColor(Vector3.Zero, Constants.VectorColor);

            for (var i = 1; i < _explodeVectors.Length; i++)
            {
                _currentExplosionVectors[i] += _explodeVectors[i];
                vertices[i] = new VertexPositionColor(_currentExplosionVectors[i], Constants.VectorColor);
            }

            SetVerticies(vertices);

            _currentExplodeStep++;

            if (_currentExplodeStep == 20)
                ShouldDraw = false;
        }
    }
}
